package com.mv.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mv.mapper.CardMapper;
import com.mv.mapper.ExchangeMapper;
import com.mv.mapper.WaresMapper;
import com.mv.pojo.Card;
import com.mv.pojo.Exchange;
import com.mv.pojo.Wares;
import com.mv.vo.Form;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ExchangeServiceImpl implements ExchangeService{
    @Autowired
    private CardService cardService;
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private WaresMapper waresMapper;
    @Autowired
    private ExchangeMapper exchangeMapper;
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
    @Override
    public List<Exchange> getExchangeList() {
        int i=0;
        List<Exchange> exchangeList = new ArrayList<>();
        Map<Date,List<Exchange>> map=getMap();
        for (Date date : map.keySet()) {
            exchangeList.add(
                    new Exchange().setId(i--).setDate(date).setChildren(map.get(date)));
        }
        return exchangeList;
    }
    @Transactional
    @Override
    public void saveExchange(Form form) {
        Card card = form.getCard();
        Wares wares = form.getWares();
        card.setIntegral(card.getIntegral()-card.getIntegralSum());
        cardMapper.updateById(card);
        wares.setCount(wares.getCount()-wares.getNum());
        waresMapper.updateById(wares);
        Exchange exchange = new Exchange();
        exchange.setCardId(card.getId())
                .setWaresId(wares.getId())
                .setNum(wares.getNum())
                .setExchangeTime(new Date());
        exchangeMapper.insert(exchange);
    }

    private Map<Date, List<Exchange>> getMap() {
        Map<Date,List<Exchange>> map=new HashMap<>();
        List<Exchange> exchangeList=findExchangeList();
        for (Exchange exchange : exchangeList) {
            Date date = exchange.getDate();
            if(map.containsKey(date)){
                map.get(date).add(exchange);
            }else {
                ArrayList<Exchange> list = new ArrayList<>();
                list.add(exchange);
                map.put(date,list);
            }
        }
        return map;
    }

    private List<Exchange> findExchangeList() {
        List<Exchange> exchangeList = exchangeMapper.selectList(null);
        for (Exchange exchange : exchangeList) {
            Card card = cardService.getCardById(exchange.getCardId());
            String userName = cardService.getUserNameByUserId(card.getUserId());
            Wares wares = waresMapper.selectById(exchange.getWaresId());
            String[] json = ExchangeServiceImpl.format
                    .format(exchange.getExchangeTime()).split(" ");
            Date date=null;
            Date time=null;
            try {
                date = dateFormat.parse(json[0]);
                time = timeFormat.parse(json[1]);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            exchange.setIntegralChange(exchange.getNum()*wares.getIntegral())
                    .setWaresName(wares.getWaresName())
                    .setUserName(userName)
                    .setDate(date)
                    .setTime(time);
        }
        return exchangeList;
    }
}
